//
//  MainTabBarController.m
//  Weibo
//
//  Created by Justsoso on 16/4/22.
//  Copyright © 2016年 Justsoso. All rights reserved.
//

#import "MainTabBarController.h"

#import "ProfileViewController.h"
#import "DiscoverViewController.h"
#import "MessageViewController.h"
#import "HomeViewController.h"

@interface MainTabBarController ()

@end

@implementation MainTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 1. 设置当前控制器下的tabBar的颜色
    self.tabBar.tintColor = [UIColor orangeColor];
    
    // 2. 添加子视图控制器
    [self addChildViewController];
    
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    // 3. 添加中间按钮
    [self addComposeBtn];
}

// 添加子视图控制器
- (void)addChildViewController
{
    
    // 改成读取JSON，根据数据创建视图控制器
    NSString *path = [[NSBundle mainBundle] pathForResource:@"MainVCSettings" ofType:@"json"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    
    NSArray *arr = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
    
    for (NSDictionary *dict in arr)
    {
        NSLog(@"%@ %@ %@",dict[@"vcName"],dict[@"title"],dict[@"imageName"]);
        [self addChildViewController:dict[@"vcName"] imageName:dict[@"imageName"] title:dict[@"title"]];
    }
    
//    [self addChildViewController:[[HomeViewController alloc] init] imageName:@"tabbar_home" title:@"首页"];
//    [self addChildViewController:[[MessageViewController alloc] init] imageName:@"tabbar_message_center" title:@"消息"];
//    [self addChildViewController:[[UIViewController alloc] init] imageName:@"" title:@""];
//    [self addChildViewController:[[DiscoverViewController alloc] init] imageName:@"tabbar_discover" title:@"发现"];
//    [self addChildViewController:[[ProfileViewController alloc] init] imageName:@"tabbar_profile" title:@"我"];
}


// 封装
//- (void)addChildViewController:(UIViewController *)vc  <= 修改
//                     imageName:(NSString *)imageName
//                         title:(NSString *)title
- (void)addChildViewController:(NSString *)vcName
                     imageName:(NSString *)imageName
                         title:(NSString *)title
{
    
    // 由类名(字符串) => 对象
    UIViewController *vc = [[NSClassFromString(vcName) alloc] init];
    
    // 1. 设置其tabberItem上的图标
    vc.tabBarItem.image = [UIImage imageNamed:imageName];
    vc.tabBarItem.selectedImage = [UIImage imageNamed:[imageName stringByAppendingString:@"_highlighted"]];
    // 2. 设置其tabberItem上的文字
//    vc.tabBarItem.title = title;
    // 3. 将其嵌入到一个导航栏控制器中
    UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:vc];
    // 4. 设置导航栏上的标题
//    navi.navigationItem.title = title
    // 注: 2. 和 4. 如果是一样的，可以用一下一行代替
    vc.title = title;

    // 5. 将导航栏控制器添加到tabBar
    [self addChildViewController:navi];
}


- (void)addComposeBtn
{
    UIButton *btn = [[UIButton alloc] init];

    // 图片
    [btn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];

    [btn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
    [btn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];

    int width = [UIScreen mainScreen].bounds.size.width / self.viewControllers.count;
    btn.frame = CGRectMake(width * 2, 0, width, 49);

    [btn addTarget:self action:@selector(composeBtnPressed) forControlEvents:UIControlEventTouchUpInside];
    // 添加到 tabBar
    [self.tabBar addSubview:btn];
}

- (void)composeBtnPressed
{
    NSLog(@"%s",__func__);
}
@end
